using System.Web.Services;
using System;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace Codeplex.STSDTS.Services
{
    [WebService(Namespace = "http://stsdts.codeplex.com")]
    public class TestService
    {

        private readonly SPFarm _localFarm = SPFarm.Local;

        /// <summary>
        /// Checks if solution with given ID is installed
        /// </summary>
        /// <param name="solutionId">ID of the solution</param>
        /// <returns>True if installed</returns>
        [WebMethod]
        public bool SolutionExists(Guid solutionId)
        {
            var solution = _localFarm.Solutions[solutionId];
            return solution != null;
        }

        /// <summary>
        /// Checks if a solution is deployed on a given Url
        /// </summary>
        /// <param name="solutionId">ID of the solution to check</param>
        /// <param name="webApplicationUrl">Url of the webapplication</param>
        /// <returns>True if deployed on given webapplication</returns>
        [WebMethod]
        public bool SolutionDeployedOnWebApplication(Guid solutionId, string webApplicationUrl)
        {
            var solution = _localFarm.Solutions[solutionId];
            //Return false if not deployed
            if (!solution.Deployed)
                return false;
            //Return true if globally deployed
            if (solution.DeploymentState == SPSolutionDeploymentState.GlobalDeployed)
                return true;
            //Lookup webapplication for the given url
            var webApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl));
            //Loop and check if IDs do match, return true if so
            foreach (var webApplicationToCompare in solution.DeployedWebApplications)
            {
                if(webApplication.Id == webApplicationToCompare.Id)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks if a feature is activated on a given Url
        /// </summary>
        /// <param name="featureId">ID of the feature to check</param>
        /// <param name="webApplicationUrl">Url of the webapplication</param>
        /// <returns>True if activated on given webapplication</returns>
        [WebMethod]
        public bool FeatureActivatedOnWebApplication(Guid featureId, string webApplicationUrl)
        {
            //Lookup webapplication for the given url
            var webApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl));
            var feature = webApplication.Features[featureId];
            return feature != null;
        }

        /// <summary>
        /// Checks if a feature is activated on a given Url
        /// </summary>
        /// <param name="featureId">ID of the feature to check</param>
        /// <param name="siteCollectionUrl">Url of the sitecollection</param>
        /// <returns>True if deployed on given sitecollection</returns>
        [WebMethod]
        public bool FeatureActivatedOnSiteCollection(Guid featureId, string siteCollectionUrl)
        {
            //Lookup sitecollection for the given url
            using (var siteCollection = new SPSite(siteCollectionUrl))
            {
                var feature = siteCollection.Features[featureId];
                if(feature != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks if a feature is activated on a given Url
        /// </summary>
        /// <param name="featureId">ID of the feature to check</param>
        /// <param name="siteUrl">Url of the site</param>
        /// <returns>True if deployed on given sitecollection</returns>
        [WebMethod]
        public bool FeatureActivatedOnSite(Guid featureId, string siteUrl)
        {
            //Lookup sitecollection for the given url
            using (var siteCollection = new SPSite(siteUrl))
            {
                var relUrl = siteUrl.Replace(siteCollection.Url, string.Empty);
                using(var site = siteCollection.OpenWeb(relUrl))
                {
                    var feature = site.Features[featureId];
                    if (feature != null)
                        return true;
                }
                
            }
            return false;
        }

    }
}